package com.xiaoq.shop.endpoint;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.collect.Lists;
import com.xiaoq.BaseEndpoint2B;
import com.xiaoq.RedisTemplate4JedisCluster;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.order.entity.PayState;
import com.xiaoq.order.entity.ShopOrderEntity;
import com.xiaoq.order.entity.ShoppingItem;
import com.xiaoq.order.service.IShopOrderService;
import com.xiaoq.order.vo.OrderQueryVo;
import com.xiaoq.shop.entity.CatalogEntity;
import com.xiaoq.shop.entity.SKUEntity;
import com.xiaoq.shop.entity.SPUCatalog;
import com.xiaoq.shop.entity.SPUCatalog.TicketType;
import com.xiaoq.shop.entity.SPUEntity;
import com.xiaoq.shop.service.ICatalogService;
import com.xiaoq.shop.service.ISKUService;
import com.xiaoq.shop.service.ISPUService;
import com.xiaoq.shop.vo.SKUTicketDetailVo;
import com.xiaoq.shop.vo.SKUTicketVo;
import com.xiaoq.vo.PagingVo;
import io.swagger.annotations.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.map.MultiKeyMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by chenjh on 2017/11/3.
 */
@RestController
@RequestMapping(value = "/api/v1/shops")
@Api(tags={"门票管理Api"})
public class TicketEndpoint2B extends BaseEndpoint2B{

    private static final class SPUCatalogResponse extends GenericDataResponse<SPUCatalog> {}
    private static final class SKUTicketResponse extends GenericDataResponse<SKUTicketVo> {}
    private static final class SKUTicketListResponse extends GenericListResponse<SKUTicketVo> {}

    @Autowired
    private ISKUService skuService;

    @Autowired
    private ICatalogService catalogService;

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private ISPUService spuService;

    @Autowired
    private IShopOrderService orderService;

    @Autowired
    private RedisTemplate4JedisCluster redisTemplate4JedisCluster;

    @ApiOperation(value="查询门票的种类", notes="查询门票的种类")
    @ApiResponses({
            @ApiResponse(code = 200, response = SPUCatalogResponse.class, message = "查询门票的种类")
    })
    @GetMapping(value="/ticketcatalog", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> ticketCatalogList() {
        CatalogEntity catalog = catalogService.getTicketCatalogList();
        return SPUCatalogResponse.okWithData(catalog.getSpuCatalogs());
    }

    @ApiOperation(value="添加门票", notes="添加门票")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "添加门票")
    })
    @PostMapping(value="/shop/tickets", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> addTickets(HttpServletRequest request,
                                        @ApiParam(value="ticketVo", required=true) @RequestBody SKUTicketVo ticketVo) {
        if (ticketVo == null) {
            return GenericResponse.ng("RequestBody 不能为空");
        }

        String mgrId = getMgrId();
        String shopId = getShopId();
        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }

        //TODO 暂不做user的校验
        String spuId = idGenerator.nextId();
        String skuId = idGenerator.nextId();
        String spuCatalogCode = idGenerator.nextId();
        String spuCode = idGenerator.nextId();
        String skuCode = idGenerator.nextId();

        SPUEntity spuEntity = spuService.getByShopIdAndSpuName(shopId, ticketVo.getSpuName());
        if (spuEntity == null || StringUtils.isBlank(spuEntity.getId())) {
            //不存在此spu，直接save
            SPUEntity newSpuEntity = new SPUEntity();
            newSpuEntity.setId(spuId);
            newSpuEntity.setCatalogCode("0"); //写死门票分类
            newSpuEntity.setCatalogName("门票");
            newSpuEntity.setSaleState(ConstantUtil.SALE_STATE_ON);
            newSpuEntity.setShopId(shopId);
            newSpuEntity.setSpuCatalogCode(spuCatalogCode);
            newSpuEntity.setSpuCode(spuCode);
            newSpuEntity.setSpuCatalogName(ticketVo.getSpuName());//票的名称
            newSpuEntity.setSpuName(ticketVo.getSpuName());//票的名称

            SKUEntity newSkuEntity = new SKUEntity();
            newSkuEntity.setId(skuId);
            newSkuEntity.setBookNotices(ticketVo.getBookNotices());
            newSkuEntity.setDiscountPrice(new Double(ticketVo.getDiscountPrice() * 100).longValue());
            newSkuEntity.setPrice(new Double(ticketVo.getPrice() * 100).longValue());
            newSkuEntity.setTicketLabels(ticketVo.getTicketLabels());
            newSkuEntity.setMax2PermitPurchased(ticketVo.getMax2PermitPurchased());
            newSkuEntity.setPreSalePeriod(ticketVo.getPreSalePeriod());
            newSkuEntity.setPromotionState(2); //限时折扣
            newSkuEntity.setShopId(shopId);
            newSkuEntity.setSaleState(ConstantUtil.SALE_STATE_ON);
            newSkuEntity.setStocks(ticketVo.getStocks());//库存
            newSkuEntity.setSkuCode(skuCode);
            newSkuEntity.setSpecifications(ticketVo.getSpecifications());
            newSkuEntity.setSpuName(ticketVo.getSpuName());
            newSkuEntity.setSpuCode(spuCode);
            newSkuEntity.setSellPlatform(ticketVo.getSellPlatform());
            newSkuEntity.setSellPlatformGoodsId(ticketVo.getSellPlatformGoodsId());

            spuService.save(newSpuEntity);
            skuService.save(newSkuEntity);
            return GenericResponse.ok();
        } else { //spu存在
            SKUEntity skuEntity = skuService.findTicketSku(shopId, ticketVo.getSpuName(), ticketVo.getSpecifications());
            if (skuEntity != null && StringUtils.isNotBlank(skuEntity.getId())) {
                return GenericResponse.ng("此种票【" + ticketVo.getSpuName() + ticketVo.getSpecifications() + "】已存在");
            }

            //将newSkuEntity保存到spuEntity下
            SKUEntity newSkuEntity = new SKUEntity();
            newSkuEntity.setId(skuId);
            newSkuEntity.setBookNotices(ticketVo.getBookNotices());
            newSkuEntity.setDiscountPrice(new Double(ticketVo.getDiscountPrice() * 100).longValue());
            newSkuEntity.setPrice(new Double(ticketVo.getPrice() * 100).longValue());
            newSkuEntity.setTicketLabels(ticketVo.getTicketLabels());
            newSkuEntity.setMax2PermitPurchased(ticketVo.getMax2PermitPurchased());
            newSkuEntity.setPreSalePeriod(ticketVo.getPreSalePeriod());
            newSkuEntity.setPromotionState(2); //限时折扣
            newSkuEntity.setShopId(shopId);
            newSkuEntity.setSaleState(ConstantUtil.SALE_STATE_ON);
            newSkuEntity.setStocks(ticketVo.getStocks());//库存
            newSkuEntity.setSkuCode(skuCode);
            newSkuEntity.setSpecifications(ticketVo.getSpecifications());
            newSkuEntity.setSpuName(ticketVo.getSpuName());
            newSkuEntity.setSpuCode(spuEntity.getSpuCode());
            newSkuEntity.setSellPlatform(ticketVo.getSellPlatform());
            newSkuEntity.setSellPlatformGoodsId(ticketVo.getSellPlatformGoodsId());

            skuService.save(newSkuEntity);
            return GenericResponse.ok();
        }
    }

    @ApiOperation(value="查看门票", notes="查看门票")
    @ApiResponses({
            @ApiResponse(code = 200, response = SKUTicketResponse.class, message = "查看门票")
    })
    @GetMapping(value="/shop/tickets/{skuId}", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> addTickets(HttpServletRequest request,
                                        @ApiParam(value="skuId", required=true) @PathVariable String skuId) {
        SKUEntity skuEntity = skuService.getTicketSkuById(skuId);
        if(skuEntity == null){
            return GenericResponse.ng("该票不存在");
        }
        String mgrId = (String) request.getAttribute("userId");
        String shopId = redisTemplate4JedisCluster.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, mgrId);
        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }
        SKUTicketVo skuTicket = new SKUTicketVo();
        BeanUtils.copyProperties(skuEntity, skuTicket);
        skuTicket.setPrice(skuEntity.getPrice()/100.D);
        skuTicket.setDiscountPrice(skuEntity.getDiscountPrice()/100.D);

        return SKUTicketResponse.okWithData(skuTicket);
    }

    @ApiOperation(value="修改门票", notes="修改门票")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "修改门票")
    })
    @PutMapping(value="/tickets", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> updateTickets(HttpServletRequest request,@ApiParam(value="ticketVo", required=true) @RequestBody SKUTicketVo ticketVo) {
        if(ticketVo == null || StringUtils.isBlank(ticketVo.getId())){
            return GenericResponse.ng("RequestBody 不能为空");
        }

        String mgrId = (String) request.getAttribute("userId");
        String shopId = redisTemplate4JedisCluster.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, mgrId);
        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }

        //1、不能修改【商品分类】或【商品名称】
        SKUEntity skuEntity = skuService.getTicketSkuById(ticketVo.getId());
        if(skuEntity == null){
            return GenericResponse.ng("不存在" + ticketVo.getId() + "的票");
        }
        if((!StringUtils.equals(skuEntity.getSpuName(), ticketVo.getSpuName()))
                || (!StringUtils.equals(skuEntity.getSpecifications(), ticketVo.getSpecifications()))){
            return GenericResponse.ng("不能修改【商品分类】或【商品名称】");
        }

        SKUEntity newSkuEntity = new SKUEntity();
        newSkuEntity.setId(ticketVo.getId());
        newSkuEntity.setBookNotices(ticketVo.getBookNotices());
        newSkuEntity.setDiscountPrice(new Double(ticketVo.getDiscountPrice()*100).longValue());
        newSkuEntity.setPrice(new Double(ticketVo.getPrice()*100).longValue());
        newSkuEntity.setTicketLabels(ticketVo.getTicketLabels());
        newSkuEntity.setMax2PermitPurchased(ticketVo.getMax2PermitPurchased());
        newSkuEntity.setPreSalePeriod(ticketVo.getPreSalePeriod());
        newSkuEntity.setPromotionState(2); //限时折扣
        newSkuEntity.setShopId(shopId);
        //newSkuEntity.setSaleState(ticketVo.getSaleState());
        newSkuEntity.setStocks(ticketVo.getStocks());//库存
        newSkuEntity.setSortIndex(ticketVo.getSortIndex());

        newSkuEntity.setSellPlatform(ticketVo.getSellPlatform());
        newSkuEntity.setSellPlatformGoodsId(ticketVo.getSellPlatformGoodsId());

        boolean retFlag = skuService.updateSkuById(newSkuEntity);

        if(retFlag){
            return GenericResponse.ok();
        }
        return GenericResponse.ng("修改门票失败");
    }

    @ApiOperation(value="修改门票序号", notes="修改门票序号")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "修改门票序号")
    })
    @PutMapping(value="/tickets/{id}/sortindex", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> updateTicketsIndex(HttpServletRequest request,
                                                @ApiParam(value="id", required=true) @PathVariable String id,
                                                @ApiParam(value="sortIndex", required=true) @RequestParam int sortIndex) {

        String mgrId = (String) request.getAttribute("userId");
        String shopId = redisTemplate4JedisCluster.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, mgrId);
        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }

        SKUEntity skuEntity = skuService.getTicketSkuById(id);
        if(skuEntity == null){
            return GenericResponse.ng("不存在" + id + "的票");
        }

        SKUEntity newSkuEntity = new SKUEntity();
        newSkuEntity.setId(id);
        newSkuEntity.setSortIndex(sortIndex);
        boolean retFlag = skuService.updateSkuById(newSkuEntity);
        if(retFlag){
            return GenericResponse.ok();
        }
        return GenericResponse.ng("修改门票序号失败");
    }

    @ApiOperation(value="复制门票", notes="复制门票")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "复制门票")
    })
    @GetMapping(value="/copytickets/{skuId}", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> copytickets(HttpServletRequest request,@ApiParam(value="skuId", required=true) @PathVariable String skuId) {

        String shopId = getShopId();

        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }

        //复制门票，只能复制SKU
        SKUEntity skuEntity = skuService.getTicketSkuById(skuId);
        if(skuEntity == null){
            return GenericResponse.ng("该票(sku)不存在");
        }
        List<String> ticketTypeCodeList = new ArrayList<>();
        CatalogEntity catalog = catalogService.getTicketCatalogList();
        if(catalog != null){
            if(CollectionUtils.isNotEmpty(catalog.getSpuCatalogs())){
                List<TicketType> ticketTypes = catalog.getSpuCatalogs().get(0).getTicketTypes();
                if(CollectionUtils.isNotEmpty(ticketTypes)){
                    ticketTypeCodeList = ticketTypes.stream().map(item -> item.getCode()).collect(Collectors.toList());
                }
            }
        }

        List<SKUEntity> skuEntityList = skuService.getSkuList(skuEntity.getShopId(), skuEntity.getSpuCode(),ConstantUtil.SALE_STATE_ON);
        List<String> spespecificationsList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(skuEntityList)){
            for(SKUEntity sku : skuEntityList){
                spespecificationsList.add(sku.getSpecifications());
            }
        }

        Set<String> ticketTypeSet = new HashSet<>(ticketTypeCodeList);
        Set<String> specificationsSet = new HashSet<>(spespecificationsList);
        Collection<String> remainSet = CollectionUtils.subtract(ticketTypeSet, specificationsSet);
        List<String> remainList = new ArrayList<>(remainSet);
        if(CollectionUtils.isNotEmpty(remainList)){
            //新建SKU
            //将newSkuEntity保存到spuEntity下
            SKUEntity newSkuEntity = new SKUEntity();
            BeanUtils.copyProperties(skuEntity, newSkuEntity);
            newSkuEntity.setId(idGenerator.nextId());
            newSkuEntity.setSaleState(ConstantUtil.SALE_STATE_OFF);
            newSkuEntity.setSellPlatformGoodsId(null);
            newSkuEntity.setSellPlatform(null);
            newSkuEntity.setSpecifications(remainList.get(0));
            skuService.save(newSkuEntity);

            return GenericResponse.ok();
        }

        return GenericResponse.ng("复制失败，票的商品分类已用完");
    }

    @ApiOperation(value="门票列表查询", notes="门票列表查询")
    @ApiResponses({
            @ApiResponse(code = 200, response = SKUTicketListResponse.class, message = "门票列表查询")
    })
    @GetMapping(value="/tickets", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> listTickets(HttpServletRequest request,
                                         @ApiParam(value="saleStatus(-1(全部),0(仓库中),1(出售中))", required=false, allowableValues = "-1,0,1") @RequestParam(required = false, defaultValue = "-1") int saleStatus,
                                         @ApiParam(value="pn", required=false) @RequestParam(required = false, defaultValue = "1") int pn,
                                         @ApiParam(value="ps", required=false) @RequestParam(required = false, defaultValue = "10") int ps) {

        String shopId = getShopId();
        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }
        PagingVo pagingVo = new PagingVo(pn, ps);
        List<SKUEntity> skuEntityList = skuService.getTicketSkuList(shopId, pagingVo, saleStatus);
        long totalCount = skuService.getTicketSkuListCount(shopId, pagingVo, saleStatus);

        if(CollectionUtils.isEmpty(skuEntityList)){
            return SKUTicketListResponse.listAndCount(new ArrayList<>(), totalCount);
        }

        List<String> spuCodeList = Lists.newArrayList();
        List<String> skuCodeList = Lists.newArrayList();
        MultiKeyMap spuSku2SkuEntityMap = new MultiKeyMap();
        for(SKUEntity skuEntity:skuEntityList){
            String spuCode = skuEntity.getSpuCode();
            String skuCode = skuEntity.getSkuCode();

            skuCodeList.add(skuCode);
            spuCodeList.add(spuCode);

            spuSku2SkuEntityMap.put(spuCode, skuCode, skuEntity);
        }

        //spuCode skuCode 为key，销售的数量为value
        MultiKeyMap spuSku2NumMap = new MultiKeyMap();

        //create Query
        OrderQueryVo orderQueryVo = new OrderQueryVo();
        orderQueryVo.setPayState(PayState.PAID.value());
        orderQueryVo.setOrderType(ConstantUtil.ORDER_TYPE_TICKET);
        orderQueryVo.setShopId(shopId);
        orderQueryVo.setSpuCodeList(spuCodeList);
        List<ShopOrderEntity> orderEntityList = orderService.getShopAllOrders(orderQueryVo, null);
        if(CollectionUtils.isNotEmpty(orderEntityList)){
            //取出购买的数量，进行统计
            for(ShopOrderEntity orderEntity:orderEntityList){
                List<ShoppingItem> shoppingItemList = orderEntity.getShoppingItems();
                if(CollectionUtils.isNotEmpty(shoppingItemList)){
                    for(ShoppingItem shoppingItem:shoppingItemList){
                        String spuCode = shoppingItem.getSpuCode();
                        String skuCode = shoppingItem.getSkuCode();
                        Integer sellNum = (Integer) spuSku2NumMap.get(spuCode, skuCode);
                        if(sellNum == null){
                            spuSku2NumMap.put(spuCode, skuCode, shoppingItem.getNumber());
                        }else {
                            spuSku2NumMap.put(spuCode, skuCode, shoppingItem.getNumber() + sellNum);
                        }
                    }
                }
            }
        }

        Map<String, String> ticketCode2TypeNameMap = catalogService.getTicketCode2TypeMap();
        if(MapUtils.isEmpty(ticketCode2TypeNameMap)){
            ticketCode2TypeNameMap = new HashMap<>();
        }

        List<SKUTicketVo> resultList = new ArrayList<>();
        SKUTicketVo skuTicketVo;
        for(SKUEntity skuEntity:skuEntityList){
            skuTicketVo = new SKUTicketVo();

            BeanUtils.copyProperties(skuEntity, skuTicketVo);
            Integer sellNum = (Integer) spuSku2NumMap.get(skuEntity.getSpuCode(), skuEntity.getSkuCode());
            skuTicketVo.setTotalSale(sellNum==null?0:sellNum);
            skuTicketVo.setPrice(skuEntity.getPrice()/100.0);
            skuTicketVo.setDiscountPrice(skuEntity.getDiscountPrice()/100.0);
            skuTicketVo.setSpecificationsName(ticketCode2TypeNameMap.get(skuTicketVo.getSpecifications()));
            resultList.add(skuTicketVo);
        }

        return SKUTicketListResponse.listAndCount(resultList, totalCount);
    }

    @ApiOperation(value="删除门票信息", notes="删除门票信息")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "删除门票信息")
    })
    @DeleteMapping(value="/tickets", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> deleteTickets(HttpServletRequest request, @ApiParam(name = "ticketIds", value="票的id数组", required=true) @RequestBody List<TicketVo> ticketVos) {

        if(CollectionUtils.isEmpty(ticketVos)){
            return GenericResponse.ng("删除的门票Id不能为空");
        }

        String shopId = getShopId();

        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }

        List<String> skuIds = new ArrayList<>();
        List<String> spuCodeList = new ArrayList<>();
        for(TicketVo ticketVo : ticketVos){
            skuIds.add(ticketVo.getId());
            spuCodeList.add(ticketVo.getSpuCode());
        }

        //根据skuId 主键删除SKU信息
        boolean deleteFlag = skuService.deleteSkuByShopIdAndSkuIds(shopId, skuIds);

        if(deleteFlag){
            //sku删除，查看是否还有spu关联sku，如果没有，删除此spu
            List<SKUEntity> skuList = skuService.getSkuList(shopId, spuCodeList, null);

            boolean deleteSpuFlag = true;

            if(skuList != null){
                List<String> existSpucodeList = skuList.stream().map(item->item.getSpuCode()).collect(Collectors.toList());
                List<String> deleteSpucodeList = (List<String>)CollectionUtils.subtract(spuCodeList, existSpucodeList);
                //删除spu
                if(CollectionUtils.isNotEmpty(deleteSpucodeList)){
                    deleteSpuFlag = spuService.deleteByShopIdAndSpucodeList(shopId, deleteSpucodeList);
                }
                if(!deleteSpuFlag){
                    logger.error("删除spu失败 shopId: {}, spuCodeList:{}, existSpucodeList:{},deleteSpucodeList:{} ", shopId, spuCodeList,existSpucodeList,deleteSpucodeList);
                }
            }

            return GenericResponse.ok();
        }
        return GenericResponse.ng("删除门票失败");
    }


    @ApiOperation(value="门票下架", notes="门票下架")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "门票下架")
    })
    @PutMapping(value="/tickets/offsale", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> ticketsOffSale(HttpServletRequest request,@ApiParam(name = "ticketIds", value="票的id数组", required=true) @RequestBody List<TicketVo> ticketVos) {

        if(CollectionUtils.isEmpty(ticketVos)){
            return GenericResponse.ng("要下架的门票Id不能为空");
        }

        String shopId = getShopId();
        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }

        List<String> skuIds = new ArrayList<>();
        List<String> spuCodeList = new ArrayList<>();
        for(TicketVo ticketVo : ticketVos){
            skuIds.add(ticketVo.getId());
            spuCodeList.add(ticketVo.getSpuCode());
        }

        //根据skuId 主键删除SKU信息
        boolean updateFlag = skuService.updateSkuSaleStateByShopIdAndSkuIds(shopId, skuIds, ConstantUtil.SALE_STATE_OFF);
        if(updateFlag){
            return GenericResponse.ok();
        }

        return GenericResponse.ng("修改门票下架失败");
    }

    @ApiOperation(value="门票上架", notes="门票上架")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "门票上架")
    })
    @PutMapping(value="/tickets/onsale", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> ticketsOnSale(HttpServletRequest request,@ApiParam(name = "ticketIds", value="票的id数组", required=true) @RequestBody List<TicketVo> ticketVos) {

        if(CollectionUtils.isEmpty(ticketVos)){
            return GenericResponse.ng("要下架的门票Id不能为空");
        }

        String shopId = getShopId();
        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("未取到登录用户的商店ID");
        }

        List<String> skuIds = new ArrayList<>();
        List<String> spuCodeList = new ArrayList<>();
        for(TicketVo ticketVo : ticketVos){
            skuIds.add(ticketVo.getId());
            spuCodeList.add(ticketVo.getSpuCode());
        }

        boolean updateFlag = skuService.updateSkuSaleStateByShopIdAndSkuIds(shopId, skuIds, ConstantUtil.SALE_STATE_ON);
        if(updateFlag){
            return GenericResponse.ok();
        }

        return GenericResponse.ng("修改门票上架失败");
    }


    @ApiOperation(value="渠道分销-所有门票", notes="渠道分销-所有门票")
    @ApiResponses({
            @ApiResponse(code = 200, response = SKUTicketListResponse.class, message = "渠道分销-所有门票")
    })
    @GetMapping(value="/tickets4distribution", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> ticketsList() {

        String shopId = getShopId();
        PagingVo pagingVo = new PagingVo(1, 100); //一般景区票的种类少，暂取100
        List<SKUEntity> skuList = skuService.getTicketSkuList(shopId, pagingVo, ConstantUtil.SALE_STATE_ON);

        List<SKUTicketVo> resultList = new ArrayList<>();
        SKUTicketVo skuTicketVo;
        if(CollectionUtils.isNotEmpty(skuList)){
            for (SKUEntity skuEntity : skuList){
                skuTicketVo = new SKUTicketVo();
                BeanUtils.copyProperties(skuEntity, skuTicketVo);
                resultList.add(skuTicketVo);
            }
        }

        return SKUTicketListResponse.listNoCount(resultList);
    }


        @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
        @JsonInclude(value = JsonInclude.Include.NON_NULL)
        @JsonIgnoreProperties(ignoreUnknown = true)
        public static class TicketVo {
            @ApiModelProperty(name = "id", value = "sku 主键")
            private String id; //sku 主键
            @ApiModelProperty(name = "spuCode", value = "spuCode", position=10)
            private String spuCode;
            @ApiModelProperty(name = "skuCode", value = "skuCode", position=20)
            private String skuCode;
            public String getId() {
                return id;
            }
            public void setId(String id) {
                this.id = id;
            }
            public String getSpuCode() {
                return spuCode;
            }
            public void setSpuCode(String spuCode) {
                this.spuCode = spuCode;
            }
            public String getSkuCode() {
                return skuCode;
            }
            public void setSkuCode(String skuCode) {
			this.skuCode = skuCode;
		}
    }

}